package com.wy.controller;

import com.wy.model.Result;
import com.wy.model.Room;
import com.wy.serivce.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/12/510:39
 */
@RestController
@RequestMapping("room")
//@CacheConfig(cacheNames = "room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Cacheable(value = "room1",key = "#pagenum+'-'+#pagesize")
    @GetMapping("list")
    public Result<Room> list(HttpServletRequest request, Room room,
                              @RequestParam(defaultValue = "1") int pagenum,
                              @RequestParam(defaultValue = "2") int pagesize){
        Page<Room> page = roomService.pagelist(room,pagenum,pagesize);
        return new Result(page.getTotalElements(),page.getContent());
    }

    @Cacheable(value = "room1")
    @GetMapping("list1")
    public List<Room> list1(){
       return roomService.getAll();
    }

    @Cacheable(value = "room1",key = "#id")
    @GetMapping("getById/{id}")
    public Room getById(@PathVariable String id){
        return roomService.getById(id);
    }

    @CacheEvict(value = "room1",key = "#room.id")
    @PostMapping("save")
    public Room save(@RequestBody Room room){
        return roomService.save(room);
    }

    @CacheEvict(value = "room1",key = "#id",allEntries=true)
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable String id){
        roomService.delete(id);
        return true;
    }
}
